package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.service.AssessmentService;
import com.itheima.utils.QiniuUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * 体质辨识
 */
@RestController
@RequestMapping("/assessment")
public class AssessmentController {
    @Reference
    private AssessmentService assessmentService;

    //分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult page = assessmentService.findPage(queryPageBean);
        return page;
    }

    //查看体检报告
    @RequestMapping("/getFileName/{fileNumber}")
    public Result getFileName(@PathVariable String fileNumber){
        try{
            String fileName = assessmentService.findFileName(fileNumber);
            return new Result(true,MessageConstant.GET_FILE_SUCCESS,fileName);
        }catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_FILE_FAIL);
        }
    }

    //根据会员档案号查询会员id，name
    @RequestMapping("/getMember/{fileNumber}")
    public Result getMember(@PathVariable String fileNumber){
        try{
            Map member = assessmentService.getMember(fileNumber);
            return new Result(true,MessageConstant.GET_MEMBERIN_SUCCESS,member);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(true,MessageConstant.GET_MEMBERIN_FAIL);
        }
    }

    //编辑会员体质状态
    @RequestMapping("/add")
    public Result add(@RequestBody Map map){
        System.out.println(map);
        try{
            assessmentService.add(map);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_MEDICAL_FAIL);
        }
        return new Result(true,MessageConstant.EDIT_MEDICAL_SUCCESS);
    }

    //根据会员档案号删除会员
    @RequestMapping("/delete/{fileNumber}")
    public Result delete(@PathVariable String fileNumber){
        try{
            Map fileNameMap = assessmentService.delete(fileNumber);
            //获取文件名
            for (Object key : fileNameMap.keySet()) {
                String fileName = (String) fileNameMap.get(key);
                //删除七牛云存储的文件
                QiniuUtils.deleteFileFromQiniu(fileName);
            }
            return new Result(true,MessageConstant.DELETE_MEMBER_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(true,MessageConstant.DELETE_MEMBER_FAIL);
        }
    }
}
